package com.lkd.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.lkd.dto.LoginDto;
import com.lkd.entity.User;
import com.lkd.exception.BusinessException;
import com.lkd.mapper.UserMapper;
import com.lkd.service.UserService;
import com.lkd.vo.LoginUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl  implements UserService {

    @Autowired
    private UserMapper userMapper;

    //用户登录
    @Override
    public LoginUserVo login(LoginDto dto) {
        if (StrUtil.isEmpty(dto.getLoginName() )|| StrUtil.isEmpty(dto.getPassword())){
            throw new BusinessException(400,"用户名和密码不能为空");
        }
        User user = userMapper.findByUsername(dto.getLoginName());
        if (user == null){
            throw new BusinessException(400,"用户名不存在");
        }
        if (!StrUtil.equals(dto.getPassword(),user.getPassword())){
            throw new BusinessException(400,"密码错误");
        }
        LoginUserVo loginUserVo = BeanUtil.toBean(user, LoginUserVo.class);
        loginUserVo.setUserId(user.getId());
        return loginUserVo;
    }
}
